package com.cc.common;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.security.KeyStore;

/**
 * 进行excel的写操作类，用于将用例结果进行写入，安装maven依赖poi、poi-ooxml
 * @author cc
 * @create 2020-04-26-16:53
 */
public class ExcelWrite {
    //操作的excel
    private Workbook workbook;
    //写入excel后需要存储的位置
    private String path;
    //该sheet的实际行数
    private int rowNum=0;
    //操作的当前的sheet
    private Sheet sheet;

    /**
     * 只进行写操作时的构造函数
     * @param path 写入excel的路径
     */
    public ExcelWrite(String path){
        if(path.endsWith(".xslx")){
            this.path = path;
            workbook = new XSSFWorkbook();
        }else if(path.endsWith(".xls")){
            this.path = path;
            workbook = new HSSFWorkbook();
        }else{
            System.out.println("输入保存地址错误，不是excel");
        }
    }

    /**
     * 复制目标的excel文件，并且进行将复制结果进行打开写操作
     * @param savePath 保存的excel路径
     * @param copyPath 复制的excel路径
     */
    public ExcelWrite(String savePath,String copyPath){
        File copyFile = new File(copyPath);
        File saveFile = new File(savePath);
        //判断复制的的路径是不是一个文件
        if(!copyFile.isFile()){
            System.out.println(copyPath+"不是文件请检查");
            return;
        }
        //复制操作
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(copyFile);
            out = new FileOutputStream(saveFile);
            //一次从复制文件流中读取1024个byte
            byte[] copyByte = new byte[1024];
            int len;
            while ((len=in.read(copyByte))!=-1){
                out.write(copyByte,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //复制完成关闭输入和输出流
        } finally {
            try {
                if(in!=null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(out!=null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileInputStream stream= null;
        try {
            stream = new FileInputStream(new File(savePath));
            ;
            //重新打开已经复制好的excel文件
            if(savePath.endsWith(".xlsx")){
                this.path = savePath;
                workbook = new XSSFWorkbook(stream);
                sheet = workbook.getSheetAt(0);
                rowNum = sheet.getPhysicalNumberOfRows();
            }else if(savePath.endsWith(".xls")){
                this.path = savePath;
                workbook = new HSSFWorkbook(stream);
                sheet = workbook.getSheetAt(0);
                rowNum = sheet.getPhysicalNumberOfRows();
            }else{
                System.out.println("不是excel文件请检查");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭输入流
            if(stream!=null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 通过名称添加sheet
     * @param name
     */
    public void addSheet(String name){
        sheet = workbook.createSheet(name);
        rowNum = 0;
    }

    /**
     * 获取sheet个数
     * @return
     */
    public int getSheetNum(){
        int sheetNum = 0;
        if(workbook!=null){
        sheetNum = workbook.getNumberOfSheets();
        }
        return sheetNum;
    }

    /**
     * 根据index切换sheet
     * @param index
     */
    public void useSheet(int index){
        if(workbook!=null){
            sheet = workbook.getSheetAt(index);
            rowNum = sheet.getPhysicalNumberOfRows();
        }
    }
    /**
     * 通过index获取sheet的名称
     * @param index
     * @return sheet的名称
     */
    public String getSheetName(int index){
        String name = "";
        if(workbook!=null){
            name = workbook.getSheetName(index);
        }
        return name;
    }

    /**
     * 向指定的单元格写入内容
     * @param rowNo 行
     * @param ColNo 列
     * @param value 写入内容
     */
    public void wirteCell(int rowNo,int ColNo,String value){
        Row row = null;
        try {
            row = sheet.getRow(rowNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //没有获取到该行，就创建
        if(row ==null){
            row = sheet.createRow(rowNo);
        }
        Cell cell = row.createCell(ColNo);
        cell.setCellValue(value);
    }

    public void save(){
        try {
            FileOutputStream stream = new FileOutputStream(new File(path));
            workbook.write(stream);
            workbook.close();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}








class TestExcelWrite{
    public static void main(String[] args){
        ExcelWrite write;
        //String copyPath = "G:\\git_project\\AutoTestFrame\\src\\HTTPLogin.xlsx";
        String savePath = "httplogin.xls";
        String copyPath = "G:\\git_project\\AutoTestFrame\\src\\main\\dailyleaderview.xls";
        write = new ExcelWrite(savePath,copyPath);
        //write = new ExcelWrite(savePath);
        write.addSheet("cc");
        write.wirteCell(2,1,"cc");
        write.save();



    }
}
